package com.xuecheng.manage_cms.controller;

import com.xuecheng.api.cms.CmsPageControllerApi;
import com.xuecheng.framework.domain.cms.CmsPage;
import com.xuecheng.framework.domain.cms.request.QueryPageRequest;
import com.xuecheng.framework.domain.cms.response.CmsPageResult;
import com.xuecheng.framework.model.response.CommonCode;
import com.xuecheng.framework.model.response.QueryResponseResult;
import com.xuecheng.framework.model.response.ResponseResult;
import com.xuecheng.manage_cms.service.PageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/cms/page")
public class CmsPageController implements CmsPageControllerApi {

    @Autowired
    PageService pageService;

    @Override
    @GetMapping("/list/{page}/{size}")
    public QueryResponseResult findList(@PathVariable("page") int page, @PathVariable("size") int size, QueryPageRequest queryPageRequest) {
//        QueryResult queryResult = new QueryResult();
//        queryResult.setTotal(2);
//        List list = new ArrayList<>();
//        CmsPage cmsPage = new CmsPage();
//        list.add(cmsPage);
//        queryResult.setList(list);
//        QueryResponseResult queryResponseResult = new QueryResponseResult(CommonCode.SUCCESS, queryResult);
//        return queryResponseResult;

        return pageService.findList(page,size,queryPageRequest);
    }


    //查找站点的全部信息，但是未完成
    @Override
    public QueryResponseResult findSiteList() {
        return pageService.findSiteList();
    }


    /**
     * 添加页面，这里的
     * @param cmsPage 使用注解是要把传过来的数据编程json
     * @return
     */
    @Override
    @PostMapping("/add")
    public CmsPageResult add( @RequestBody CmsPage cmsPage) {
        return pageService.add(cmsPage);
    }

    /**
     * 通过id查找页面
     * @param id
     * @return
     */
    @Override
    @GetMapping("/get/{id}")
    public CmsPageResult findById(@PathVariable("id") String id) {
        //调用service层
        CmsPage byId = pageService.getById(id);
        return new CmsPageResult(CommonCode.SUCCESS,byId);
    }

    /**
     * 修改页面
     * @param id
     * @param cmsPage
     * @return
     */
    @Override
    @PutMapping("/edit/{id}")
    public CmsPageResult edit(@PathVariable("id") String id, @RequestBody CmsPage cmsPage) {

        return pageService.update(id,cmsPage);
    }

    @Override
    @DeleteMapping("/del/{id}")
    public ResponseResult delete(@PathVariable("id") String id) {
        return pageService.delete(id);
    }

    @Override
    @PostMapping("/postPage/{pageId}")
    public ResponseResult post(@PathVariable("pageId") String pageId) {
        return pageService.postPage(pageId);
    }
}
